package com.linghui.videoplus.ipai.api;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.linghui.videoplus.ipai.bean.AccountBean;
import com.linghui.videoplus.ipai.bean.SinaAccessBean;

public class Preferences {

	public final static String TAG_URLS = "SP_SERVER_URLS";
	public final static String TAG_INIT_LOGIN="INIT_LOGIN_INFO";
	public final static String TAG_SINA_ACCESS="SINA_ACCESS_INFO";
	public final static String TAG_KANKAN_ACCESS="KANKAN_ACCESS_INFO";
	
	/**
	 * 清除账户信息
	 * @param mContext
	 */
	public static void cleanAccount(Context mContext){
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_KANKAN_ACCESS, Activity.MODE_PRIVATE);
		Editor editor=sPrefereces.edit();
		editor.clear();
		editor.commit();	
		sPrefereces=mContext.getSharedPreferences(TAG_SINA_ACCESS, Activity.MODE_PRIVATE);
		editor=sPrefereces.edit();
		editor.clear();
		editor.commit();
	}
	
	
	/**
	 * 
	 * @param mContext
	 * @return
	 */
	public static String getLocalUid(Context mContext){
		String uid=null;
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_KANKAN_ACCESS, Activity.MODE_PRIVATE);
		uid = sPrefereces.getString(AccountBean.TAG_UID, null);
		if(uid==null){
			sPrefereces=mContext.getSharedPreferences(TAG_SINA_ACCESS, Activity.MODE_PRIVATE);
			uid = sPrefereces.getString(SinaAccessBean.TAG_BAND_USER_ID, null);
		}
		return uid;
	}
	
	/**
	 * 
	 * @param mContext
	 * @return
	 */
	public static String getLocalLoginName(Context mContext){
		String loginName=null;
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_KANKAN_ACCESS, Activity.MODE_PRIVATE);
		loginName = sPrefereces.getString(AccountBean.TAG_EMAIL, null);
		if(loginName==null){
			sPrefereces=mContext.getSharedPreferences(TAG_SINA_ACCESS, Activity.MODE_PRIVATE);
			loginName = sPrefereces.getString(SinaAccessBean.TAG_SINA_ACCOUNT, null);
		}
		return loginName;
	}
	
	
	/**
	 * 获取本地看看账户相关信息
	 * @param mContext
	 * @return
	 */
	public static AccountBean getKanKanAccessBean(Context mContext) {
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_KANKAN_ACCESS, Activity.MODE_PRIVATE);
		String uid = sPrefereces.getString(AccountBean.TAG_UID, null);
		String name = sPrefereces.getString(AccountBean.TAG_NAME, null);
		String email = sPrefereces.getString(AccountBean.TAG_EMAIL, null);
		String password = sPrefereces.getString(AccountBean.TAG_PASSWORD, null);
		AccountBean accountBean=null;
		if(uid!=null){
			accountBean=new AccountBean();
			accountBean.setUid(uid);
			accountBean.setName(name);
			accountBean.setEmail(email);
			accountBean.setPassword(password);
		}
		return accountBean;
	}
	
	/**
	 * 保存看看验证相关信息
	 * @param mContext
	 */
	public static void saveKanKanAccessBean(Context mContext,AccountBean accountBean){
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_KANKAN_ACCESS, Activity.MODE_PRIVATE);
		Editor editor=sPrefereces.edit();
		
		editor.putString(AccountBean.TAG_UID,accountBean.getUid());
		editor.putString(AccountBean.TAG_NAME,accountBean.getName());
		editor.putString(AccountBean.TAG_EMAIL,accountBean.getEmail());
		editor.putString(AccountBean.TAG_PASSWORD,accountBean.getPassword());
		editor.commit();
	}
	
	
	/**
	 * 获取新浪微博验证相关信息
	 * @param mContext
	 * @return
	 */
	public static SinaAccessBean getSinaAccessBean(Context mContext) {
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_SINA_ACCESS, Activity.MODE_PRIVATE);
		String userId = sPrefereces.getString(SinaAccessBean.TAG_USER_ID, null);
		String userKey = sPrefereces.getString(SinaAccessBean.TAG_USER_KEY, null);
		String userSecret = sPrefereces.getString(SinaAccessBean.TAG_USER_SECRET, null);
		String bandUserId = sPrefereces.getString(SinaAccessBean.TAG_BAND_USER_ID, null);
		String sianAccount = sPrefereces.getString(SinaAccessBean.TAG_SINA_ACCOUNT, null);
		SinaAccessBean sinaAccessBean=null;
		if(userId!=null){
			sinaAccessBean=new SinaAccessBean();
			sinaAccessBean.setUserId(userId);
			sinaAccessBean.setUserKey(userKey);
			sinaAccessBean.setUserSecret(userSecret);
			sinaAccessBean.setBandUserId(bandUserId);
			sinaAccessBean.setSinaAccount(sianAccount);
		}
		return sinaAccessBean;
	}
	
	/**
	 * 保存新浪微博验证相关信息
	 * @param mContext
	 */
	public static void saveSinaAccessBean(Context mContext,SinaAccessBean sinaAccessBean){
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_SINA_ACCESS, Activity.MODE_PRIVATE);
		Editor editor=sPrefereces.edit();
		editor.putString(SinaAccessBean.TAG_SINA_ACCOUNT,sinaAccessBean.getSinaAccount());
		editor.putString(SinaAccessBean.TAG_BAND_USER_ID,sinaAccessBean.getBandUserId());
		editor.putString(SinaAccessBean.TAG_USER_ID,sinaAccessBean.getUserId());
		editor.putString(SinaAccessBean.TAG_USER_KEY,sinaAccessBean.getUserKey());
		editor.putString(SinaAccessBean.TAG_USER_SECRET,sinaAccessBean.getUserSecret());
		editor.commit();
	}
	
	/**
	 * 设置初始化登录信息
	 * @param mContext
	 */
	public static void initLoginInfo(Context mContext){
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_INIT_LOGIN, Activity.MODE_PRIVATE);
		Editor editor=sPrefereces.edit();
		editor.putString("nickName","");
		editor.commit();
	}
	
	/**
	 * 获取初始化登录信息
	 * @param mContext
	 * @return
	 */
	public static String getInitLoginInfo(Context mContext) {
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_INIT_LOGIN, Activity.MODE_PRIVATE);
		String nickName = sPrefereces.getString("nickName", null);
		return nickName;
	}
	
	
	/**
	 * 
	 * @param mContext
	 */
	public static void refreshServerUrls(Context mContext){
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_URLS, Activity.MODE_PRIVATE);
		Editor editor=sPrefereces.edit();
		editor.putString("regeditUrl","");
		editor.putString("loginUrl","");
		editor.commit();
	}
	
	
	/**
	 * 
	 * @param mContext
	 * @return
	 */
	public static String getRegeditUrl(Context mContext) {
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_URLS, Activity.MODE_PRIVATE);
		String regeditUrl = sPrefereces.getString("", null);
		return regeditUrl;

	}
	
	/**
	 * 
	 * @param mContext
	 * @return
	 */
	public static String getLoginUrl(Context mContext) {
		SharedPreferences sPrefereces=mContext.getSharedPreferences(TAG_URLS, Activity.MODE_PRIVATE);
		String loginUrl = sPrefereces.getString("", null);
		return loginUrl;

	}
	


}
